package com.scoy.honeymei.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.scoy.honeymei.R;
import com.scoy.honeymei.bean.FilmOrderBean;
import com.scoy.honeymei.utils.TimeUtil;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ClassName: NormalAdapter$
 * @Description:
 * @Author: GaoZhan
 * @CreateDate: 2019/8/26/0026$ 下午 17:03$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/8/26/0026$ 下午 17:03$
 * @UpdateRemark: 更新内容
 */
public class BookListAdapter extends RecyclerView.Adapter {
    private Context context;
    private ArrayList<FilmOrderBean.OrderBean> datalist;
    private TextView notv;

    public BookListAdapter(Context context, ArrayList<FilmOrderBean.OrderBean> datalist, TextView notv) {
        this.context = context;
        this.datalist = datalist;
        this.notv = notv;
    }

    public interface OnOneClick {
        void oneClick(int pos);
    }

    public interface OnLongClickListener {
        void onLongClick(int pos);
    }

    OnOneClick onOneClick;

    OnLongClickListener listener;

    public void setOnOneClick(OnOneClick onOneClick) {
        this.onOneClick = onOneClick;
    }


    public void setOnLongClickListener(OnLongClickListener l) {
        this.listener = l;
    }

    public interface OnTwoClick {
        void twoClick(int pos);
    }

    OnTwoClick onTwoClick;

    public void setOnTwoClick(OnTwoClick onTwoClick) {
        this.onTwoClick = onTwoClick;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_book, parent, false);
        return new BookHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        BookHolder bookHolder = (BookHolder) holder;
        FilmOrderBean.OrderBean orderBean = datalist.get(position);
        bookHolder.filmNameTv.setText(orderBean.getMovie_title());
        bookHolder.filmTimeTv.setText("时间：" + TimeUtil.getLongToDatePHP(orderBean.getStime(), "yyyy-MM-dd HH:mm"));
        bookHolder.filmAddressTv.setText("厅号：" + orderBean.getFyt_no());
        bookHolder.delBtn.setText("删除");
        int status = orderBean.getStatus();//状态，-1=已作废、0=未出票、1=已出票、2=已退票
        bookHolder.itemFilm.setVisibility(View.VISIBLE);
        if (status == -1||status == 3) {
            bookHolder.itemFilm.setBackgroundResource(R.drawable.ic_book_abanden);
        } else if (status == 1) {
            bookHolder.itemFilm.setBackgroundResource(R.drawable.ic_book_used);
        } else if (status == 0) {
            bookHolder.itemFilm.setBackgroundResource(R.drawable.ic_book_unuse);
        } else {
            bookHolder.itemFilm.setBackgroundColor(Color.parseColor("#ffffff"));
        }

        bookHolder.itemFilm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onOneClick != null) {
                    onOneClick.oneClick(position);
                }
            }
        });

//        bookHolder.itemFilm.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                if(listener != null){
//                    listener.onLongClick(position);
//                }
//                return false;
//            }
//        });

        bookHolder.delBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onLongClick(position);
                }
            }
        });


    }


    @Override
    public int getItemCount() {
        int iCount = datalist == null ? 0 : datalist.size();
        if (iCount == 0) {
            notv.setVisibility(View.VISIBLE);
        } else {
            notv.setVisibility(View.GONE);
        }
        return iCount;
    }


    class BookHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.film_name_tv)
        TextView filmNameTv;
        @BindView(R.id.film_address_tv)
        TextView filmAddressTv;
        @BindView(R.id.film_time_tv)
        TextView filmTimeTv;
        @BindView(R.id.item_film)
        ConstraintLayout itemFilm;

        @BindView(R.id.delBtn)
        TextView delBtn;

        BookHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
